#include<bits/stdc++.h>
using namespace std;

int f[201];
int a[201];
bool vis[201][201];
int pre[201];

void PRE(int x)
{
	if(pre[x])
	{
		PRE(pre[x]);
		cout<<"-"<<x;
	}
	else
		cout<<x;
}

int main()
{
	int n, x, y;
	cin>>n;
	for(int i = 1;i <= n;i++)
	{
		cin>>a[i];
		f[i] = a[i];
	}
	while(cin>>x>>y && x && y)
		vis[x][y] = 1;
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= n;j++)
		{
			if(!vis[j][i])
				continue;
			if(f[j]+a[i] > f[i])
			{
				f[i] = f[j]+a[i];
				pre[i] = j;
			}
		}
	}
	
	int k = max_element(f+1, f+n+1)-f;
	PRE(k);
	cout<<endl<<f[k];
	return 0;
}

